﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.Generic;
using System.Data;
using Spring.Data.Common;

namespace HotelApp
{
    public static class ImageCategoryDAO
    {
        #region static field

        private static AdoTemplate template = AppConfigurer.AdoTemplate;
        private static Object lockObj = new Object();
        private static string strBase = "select Category_Code, Category_Name, Category_Desc, Create_Date"
            + " from Image_Category";

        #endregion
        
        #region internal methods

        internal static void CreateCategory(ImageCategory obj)
        {
            //Check input parameter
            if (obj == null)
                throw new Exception("ImageCategoryDAO.CreateCategory: Invalid parameter");

            lock (lockObj)
            {
                //Generate ItemCode
                string strSearch = "SELECT isnull(max(Category_Code),'C000000000') FROM Image_Category ";
                string strMaxNo = template.ExecuteScalar(CommandType.Text, strSearch).ToString();
                int nNum = Int32.Parse(strMaxNo.Substring(1)) + 1;
                obj.CategoryCode = "C" + nNum.ToString("000000000");

                //Insert into table Image_Category
                string strCreate = "insert into Image_Category values(@Category_Code, @Category_Name, @Category_Desc,"
                + "@Create_Date)";

                IDbParameters para = template.CreateDbParameters();
                para.Add("Category_Code", DbType.String).Value = obj.CategoryCode;
                para.Add("Category_Name", DbType.String).Value = obj.CategoryName;
                para.Add("Category_Desc", DbType.String).Value = obj.CategoryDesc == null ? string.Empty : obj.CategoryDesc;
                para.Add("Create_Date", DbType.DateTime).Value = obj.CreateDate;

                template.ExecuteNonQuery(CommandType.Text, strCreate, para);
            }
        }

        internal static void UpdateCategory(ImageCategory obj)
        {
            //Check input parameter
            if (obj == null)
                throw new Exception("ImageCategoryDAO.UpdateCategory: Invalid parameter");

            string strSql = "update Image_Category set Category_Name=@Category_Name,"
                + " Category_Desc=@Category_Desc where Category_Code=@Category_Code";

            IDbParameters para = template.CreateDbParameters();
            para.Add("Category_Code", DbType.String).Value = obj.CategoryCode;
            para.Add("Category_Name", DbType.String).Value = obj.CategoryName;
            para.Add("Category_Desc", DbType.String).Value = obj.CategoryDesc == null ? string.Empty : obj.CategoryDesc;

            template.ExecuteNonQuery(CommandType.Text, strSql, para);
        }

        internal static void DeleteCategory(string categoryCode)
        {
            string strSql = "delete from Image_Category where Category_Code=@Category_Code";
            IDbParameters para = template.CreateDbParameters();
            para.Add("Category_Code", DbType.String).Value = categoryCode;

            template.ExecuteNonQuery(CommandType.Text, strSql, para);
        }

        #endregion

        #region public methods

        public static ImageCategory GetCategory(string categoryCode)
        {
            string strSql = strBase + " where Category_Code=@Category_Code";

            IDbParameters para = template.CreateDbParameters();
            para.Add("Category_Code", DbType.String).Value = categoryCode;

            IList<ImageCategory> lst = template.QueryWithRowMapperDelegate(CommandType.Text, strSql,
                new RowMapperDelegate<ImageCategory>(MapRow), para);

            return lst.Count > 0 ? lst[0] : null;
        }

        public static IList<ImageCategory> ListCategories()
        {
            return template.QueryWithRowMapperDelegate(CommandType.Text, strBase,
                new RowMapperDelegate<ImageCategory>(MapRow));
        }

        #endregion

        #region private methods

        private static ImageCategory MapRow(IDataReader dr, int rownumber)
        {
            ImageCategory obj = new ImageCategory();
            obj.CategoryCode = dr.IsDBNull(0) ? "" : dr.GetString(0);
            obj.CategoryName = dr.IsDBNull(1) ? "" : dr.GetString(1);
            obj.CategoryDesc = dr.IsDBNull(2) ? "" : dr.GetString(2);
            obj.CreateDate = dr.IsDBNull(3) ? DateTime.MinValue : dr.GetDateTime(3);

            return obj;
        }

        #endregion
    }
}
